/**
 * 
 */
package com.jm.report.datasource;

import java.util.Map;

import com.jm.utils.StringHelper;
import com.jm.web.data.BaseDataEntities;
import com.jm.web.data.BaseDataEntity;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;

/**
 * @author yanjie
 * Dec 2, 2011 9:42:28 AM
 */
public class BaseDataEntitiesJRDataSource implements JRDataSource{
	private BaseDataEntities rows = null;
	private int index = 0;
	private Object row = null;
	
	public BaseDataEntitiesJRDataSource(BaseDataEntities rows) {
		this.rows = rows;
		this.index = 0;
		this.row = null;
	}
	
	public boolean next() throws JRException {
		if(this.rows == null)
			return false;
		
		try {
			boolean bnext = this.index < this.rows.size();
			if(bnext){
				this.row = this.rows.get(this.index);
			}else{
				this.row = null;
			}
			this.index++;
			return bnext;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public Object getFieldValue(JRField field) throws JRException {
		if(this.rows == null)
			return "";
		try
		{
			Object object = null;
			if(row instanceof BaseDataEntity){
				object = ((BaseDataEntity)row).gpov(field.getName());
			}else if( row instanceof Map){
				object = ((Map)row).get(field.getName());
			}
			
			if(object ==null || object instanceof String)
				object = StringHelper.getValue(object,"").trim();
			return object;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return "";
		}
	}

}
